<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Max Baryshnikov aka Mephius <mb@redgraphic.com>,	          |
// | Dmitry Sheiko <sheiko@cmsdevelopment.com>	                		  |
// | Copyright (c) 2004-2005 Max Baryshnikov                              |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 24.02.05 (dd/mm/yy)
// $Id: document_identification.inc.php,v 1.3 2005/07/04 14:00:19 sheiko Exp $

srand(round(microtime(1)*10000));

function gen_key($string){
	$im=imagecreatetruecolor(strlen($string)*22.5, 25);
	imagetruecolortopalette($im, 1, 256);
	$white=imagecolorallocate($im, 221, 194, 164);

	imagefill($im, 0, 0, $white);

	$x=0;
	for ($i=0; $i<strlen($string); $i++){
		$tmp=imagecreatetruecolor(20, 20);
		$tbg=imagecolorallocate($tmp, 221, 194, 164);

		imagefill($tmp, 0, 0, $tbg);

		$color=imagecolorallocate($tmp, rand(5, 150), rand(5, 100), rand(5, 100));
		imagechar($tmp, 5, 2, 2, $string[$i], $color);
		$tmp=imagerotate($tmp, rand(-40, 40), $tbg);
		imagecopy($im, $tmp, $x, 1, 1, 1, imagesx($tmp)-1, imagesy($tmp)-1);
		$x+=imagesx($tmp)-7;
		imagedestroy($tmp);
	}
	imagecolortransparent($im, $white);
	$x+=8;
	$out=imagecreatetruecolor(strlen($string)*22.5*1.5, 45);

	$background=imagecreatefromjpeg(dirname(__FILE__) . "/usr/templates/images/bg.jpg");
	imagecopyresampled($out, $background, 0, 0, 0, 0, imagesx($out), imagesy($out), imagesx($background), imagesy($background));
	imagedestroy($background);

	imagecopyresampled($out, $im, 0, 0, 0, 0, strlen($string)*22.5*1.5, 45, $x, 25);
	imagedestroy($im);
	header("Content-Type: image/png");
	imagetruecolortopalette($out, 0, 256);
	imagepng($out);
	imagedestroy($out);
}
session_start(); 
$string = substr(md5(rand(1,100)),0,5);
$_SESSION["Md5OfGenCode"] = md5($string);
if(!$string) $string = "ERROR";
gen_key($string);
?>